# auto_handler/auto_train.py
import asyncio
import schedule
import time
import sys
from pathlib import Path

# 添加项目路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

async def main(cycles: int = 100, episodes_per_cycle: int = 10, hybrid_components: dict = None):
    """主训练循环"""
    # 导入必要的模块
    try:
        from code_generator.ai_developer import OnpharosAIDeveloper
        from trainer.training_controller import TrainingController
        from narrative.story_generator import OnpharosStoryteller
        from game_engine.game_state import FactorType, GameWorld
    except ImportError as e:
        print(f"导入错误: {e}")
        # 备用导入路径
        sys.path.append(str(project_root / "code_generator"))
        sys.path.append(str(project_root / "trainer"))
        sys.path.append(str(project_root / "narrative"))
        sys.path.append(str(project_root / "game_engine"))
        from ai_developer import OnpharosAIDeveloper
        from training_controller import TrainingController
        from story_generator import OnpharosStoryteller
        from game_state import FactorType, GameWorld

    # 创建训练配置
    config = {
        'factor_types': list(FactorType),
        'episodes_per_cycle': episodes_per_cycle,
        'max_steps_per_episode': 200,
        'learning_rate': 1e-4,
        'gamma': 0.99,
        'clip_epsilon': 0.2,
        'replay_capacity': 10000,
        'priority_alpha': 0.6,
        'priority_beta': 0.4,
        'batch_size': 64,
        'min_batch_size': 32
    }

    # 初始化训练控制器，传入混合AI组件
    online_api_key = None
    if hybrid_components and 'strategy_manager' in hybrid_components:
        # 从混合组件中获取在线API密钥
        online_service = hybrid_components.get('online_service')
        if online_service:
            online_api_key = online_service.api_key

    trainer = TrainingController(config, online_api_key=online_api_key)

    # 创建游戏世界实例
    game_world = GameWorld()
    
    # 如果提供了混合AI组件，将它们传递给训练器
    if hybrid_components:
        if 'strategy_manager' in hybrid_components:
            trainer.strategy_manager = hybrid_components['strategy_manager']
        if 'context_manager' in hybrid_components:
            trainer.context_manager = hybrid_components['context_manager']

    developer = OnpharosAIDeveloper(str(project_root))

    for cycle in range(1, cycles + 1):
        print(f"开始第{cycle}轮回训练...")
        
        # 运行训练周期
        results = await trainer.run_training_cycle(game_world, cycle)
        
        # 分析训练结果并优化代码
        if results['overall_performance'] < 0.7:  # 性能不佳时优化
            improvements = developer.generate_improvement_patch(results)
            developer.apply_code_changes(improvements)
            
        # 生成剧情报告
        storyteller = OnpharosStoryteller()
        narrative = storyteller.generate_cycle_narrative(results)
        
        # 保存并提交
        developer.save_narrative(narrative, cycle)
        developer.auto_commit_changes(f"第{cycle}轮回优化")
        
        print(f"第{cycle}轮回完成! 性能: {results['overall_performance']:.3f}")

    print(f"训练完成！总共进行了 {cycles} 个周期")

if __name__ == "__main__":
    # 默认运行100个周期，每周期10个回合
    asyncio.run(main(cycles=10, episodes_per_cycle=10, hybrid_components=None))
